package pattern.config.datasource;

import com.google.common.collect.Maps;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import pattern.constant.DefaultSystemConstant;

import javax.sql.DataSource;
import java.util.List;
import java.util.Map;

/**
 * 数据源注册
 * @author Simon
 * @date 2020/12/31 12:12
 */
@Component
public class DataSourceRegister {
    private List<String> defDatasourceKey;
    /**
     * mode.setDataSourcesKey(key)
     * @see DynamicDataSourceContextHolder 设置数据源
     * 参数为配置的数据源
     */
    @Bean
    @Primary
    public DataSource dataSource(DataSource dataSourceOne) {
        RoutingDataSource dataSource = new RoutingDataSource();
        Map<Object, Object> targetDataSources = Maps.newHashMap();
        targetDataSources.put(DefaultSystemConstant.PRIMARY_DATA_KEY, dataSourceOne);
//        targetDataSources.put("two", otherSource);
        dataSource.setTargetDataSources(targetDataSources);
        dataSource.setDefaultTargetDataSource(dataSourceOne);
        setDefDatasourceKey(List.of(DefaultSystemConstant.PRIMARY_DATA_KEY));
        return dataSource;
    }
    public List<String> getDefDatasourceKey() {
        return this.defDatasourceKey;
    }

    public void setDefDatasourceKey(List<String> defDatasourceKey) {
        this.defDatasourceKey = defDatasourceKey;
    }
}
